#!/bin/env

from abstractoutput import AbstractOutput
import os
import sys
from database import *
import log
    
class OutputDb(AbstractOutput):
    def init(self,config_file,parameter):
        which_db = parameter[4]
        conf = self.read_config(config_file)
        if which_db not in conf:
            print which_db ,"not in %s"(config_file)
            sys.exit(-1)
        host = conf[which_db]['host']
        port = conf[which_db]['port']
        db = conf[which_db]['db']
        user = conf[which_db]['user']
        password = conf[which_db]['password']
        self.db = Connection(host+":"+port,db,user,password)
        if not self.db._db:
            log.error("connected to db failed!")
            log.error("host:%s port:%s db:%s user:%s password:%s",host,port,db,user,password)
            sys.exit(-1)
        pass

    def select_and_insert(self,select_dict,table):
        t = self.select(select_dict,table)
        if t:   
            return t
        else:   
            self.insert(select_dict,{},table)
            t = self.select(select_dict,table)
            return t

    def select(self,select_value,table):
        select_where = ""
        select_list = []
        for key in select_value:
            select_where += key+"="+"%s"+" and "
            select_list.append(select_value[key])
        select_where = select_where[:-5]
        query="select * from %s where %s;"%(table,select_where)
        #print query
        return self.db.get(query,*select_list)

    def update(self,select_value,update_value,table):
        update_list = []
        update_condition = ""
        select_where = ""
        select_list = []
        for key in update_value:
            update_condition += key+"="+"%s"+","
            update_list.append(update_value[key])
        for key in select_value:
            select_where += key+"="+"%s"+" and "
            update_list.append(select_value[key])
        select_where = select_where[:-5]
        update_condition = update_condition[:-1]
        update_query="update %s set %s where %s"%(table,update_condition,select_where)
        return self.db.execute(update_query,*update_list)


    def insert(self,select_value,insert_value,table):
        insert_list = []
        field = ""
        values = ""
        for key in select_value:
            field += key+","
            values += "%s"+","
            insert_list.append(select_value[key])
        for key in insert_value:
            field += key+","
            values += "%s"+","
            insert_list.append(insert_value[key])
        field = field[:-1]
        values = values[:-1]
        insert_query="insert into %s(%s) values(%s);"%(table,field,values)
        return self.db.execute(insert_query,*insert_list)


    def insertExample(self,select_value,insert_value,table):
        select_where = ""
        select_list = []
        for key in select_value:
            select_where += key+"="+"%s"+" and "
            select_list.append(select_value[key])
        select_where = select_where[:-5]
        query="select * from %s where %s;"%(table,select_where)
        if self.db.get(query,*select_list)!= None:
            update_list = []
            update_condition = ""
            for key in insert_value:
                update_condition += key+"="+"%s"+","
                update_list.append(insert_value[key])
            for item in select_list:
                update_list.append(item)
            update_condition = update_condition[:-1]
            update_query="update %s set %s where %s"%(table,update_condition,select_where)
            self.db.execute(update_query,*update_list)
        else:
            insert_list = []
            field = ""
            values = ""
            for key in select_value:
                field += key+","
                values += "%s"+","
                insert_list.append(select_value[key])
            for key in insert_value:
                field += key+","
                values += "%s"+","
                insert_list.append(insert_value[key])

            field = field[:-1]
            values = values[:-1]
            insert_query="insert into %s(%s) values(%s);"%(table,field,values)
            self.db.execute(insert_query,*insert_list)

    def query(self,query,*parameters):
        return self.db.query(query,*parameters)

    def output(self,query,*parameters):
        return self.db.execute(query,*parameters)

    def close(self):
        if self.db:
            self.db.close()
        log.info("db close")
